{% extends "base.html" %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>
        <i class="bi bi-envelope-open me-2"></i>邮件详情
    </h2>
    <div class="btn-group">
        <a href="{{ url_for('admin.emails') }}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left me-1"></i>返回邮件列表
        </a>
        <button class="btn btn-outline-danger" onclick="deleteEmail({{ email.id }})">
            <i class="bi bi-trash me-1"></i>删除邮件
        </button>
    </div>
</div>

<!-- 邮件信息卡片 -->
<div class="card">
    <div class="card-header bg-light">
        <div class="row">
            <div class="col-md-8">
                <h4 class="mb-0">{{ email.subject }}</h4>
            </div>
            <div class="col-md-4 text-end">
                <div class="d-flex justify-content-end align-items-center">
                    {% if email.is_read %}
                    <span class="badge bg-success me-2">已读</span>
                    {% else %}
                    <span class="badge bg-warning me-2">未读</span>
                    {% endif %}
                    
                    {% if email.is_deleted_by_sender or email.is_deleted_by_recipient %}
                    <span class="badge bg-danger">已删除</span>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
    
    <div class="card-body">
        <!-- 邮件头信息 -->
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="border rounded p-3 h-100">
                    <h6 class="text-muted mb-2">
                        <i class="bi bi-person-fill me-1"></i>发件人
                    </h6>
                    <div class="d-flex align-items-center">
                        <i class="bi bi-person-circle fs-3 me-3 text-primary"></i>
                        <div>
                            <div class="fw-bold">{{ email.sender.username }}</div>
                            <div class="text-muted">{{ email.sender.email }}</div>
                            {% if email.sender.real_name %}
                            <small class="text-muted">{{ email.sender.real_name }}</small>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-6">
                <div class="border rounded p-3 h-100">
                    <h6 class="text-muted mb-2">
                        <i class="bi bi-person-check-fill me-1"></i>收件人
                    </h6>
                    <div class="d-flex align-items-center">
                        <i class="bi bi-person-circle fs-3 me-3 text-success"></i>
                        <div>
                            <div class="fw-bold">{{ email.recipient.username }}</div>
                            <div class="text-muted">{{ email.recipient.email }}</div>
                            {% if email.recipient.real_name %}
                            <small class="text-muted">{{ email.recipient.real_name }}</small>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 时间信息 -->
        <div class="row mb-4">
            <div class="col-md-12">
                <div class="border rounded p-3">
                    <h6 class="text-muted mb-2">
                        <i class="bi bi-clock me-1"></i>发送时间
                    </h6>
                    <div class="d-flex align-items-center">
                        <i class="bi bi-calendar-event fs-4 me-3 text-info"></i>
                        <div>
                            <div class="fw-bold">{{ email.sent_at.strftime('%Y年%m月%d日 %H:%M:%S') }}</div>
                            <small class="text-muted">{{ email.sent_at.strftime('%A') }}</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 邮件内容 -->
        <div class="border rounded p-4">
            <h6 class="text-muted mb-3">
                <i class="bi bi-file-text me-1"></i>邮件内容
            </h6>
            <div class="email-content">
                {{ email.content|nl2br|safe }}
            </div>
        </div>
    </div>
    
    <div class="card-footer bg-light">
        <div class="row text-muted">
            <div class="col-md-6">
                <small>
                    <i class="bi bi-hash me-1"></i>邮件ID: {{ email.id }}
                </small>
            </div>
            <div class="col-md-6 text-end">
                <small>
                    <i class="bi bi-eye me-1"></i>
                    {% if email.is_read %}
                    已于 {{ email.read_at.strftime('%Y-%m-%d %H:%M') if email.read_at else '未知时间' }} 阅读
                    {% else %}
                    未读
                    {% endif %}
                </small>
            </div>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="bi bi-exclamation-triangle text-warning me-2"></i>确认删除
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>您确定要删除这封邮件吗？</p>
                <div class="alert alert-warning">
                    <i class="bi bi-info-circle me-2"></i>
                    <strong>注意：</strong>此操作将永久删除邮件，无法恢复！
                </div>
                <div class="border rounded p-2 bg-light">
                    <strong>主题：</strong>{{ email.subject }}
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" onclick="confirmDelete()">
                    <i class="bi bi-trash me-1"></i>确认删除
                </button>
            </div>
        </div>
    </div>
</div>

<style>
.email-content {
    line-height: 1.6;
    font-size: 1rem;
    color: #333;
    background-color: #f8f9fa;
    padding: 1rem;
    border-radius: 0.375rem;
    min-height: 200px;
    white-space: pre-wrap;
    word-wrap: break-word;
}

.border {
    border-color: #dee2e6 !important;
}

.card-header {
    border-bottom: 1px solid #dee2e6;
}

.card-footer {
    border-top: 1px solid #dee2e6;
}

.badge {
    font-size: 0.8em;
}
</style>

<script>
let emailIdToDelete = null;

/**
 * 显示删除确认对话框
 */
function deleteEmail(emailId) {
    emailIdToDelete = emailId;
    const modal = new bootstrap.Modal(document.getElementById('deleteModal'));
    modal.show();
}

/**
 * 确认删除邮件
 */
function confirmDelete() {
    if (!emailIdToDelete) return;
    
    fetch(`/admin/emails/${emailIdToDelete}/delete`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-CSRFToken': '{{ csrf_token() }}'
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            // 显示成功消息
            showAlert('success', data.message);
            
            // 延迟跳转到邮件列表
            setTimeout(() => {
                window.location.href = '{{ url_for("admin.emails") }}';
            }, 1500);
        } else {
            showAlert('danger', data.message || '删除失败');
        }
    })
    .catch(error => {
        console.error('删除邮件时出错:', error);
        showAlert('danger', '删除邮件时发生错误');
    })
    .finally(() => {
        // 关闭模态框
        const modal = bootstrap.Modal.getInstance(document.getElementById('deleteModal'));
        modal.hide();
        emailIdToDelete = null;
    });
}

/**
 * 显示警告消息
 */
function showAlert(type, message) {
    const alertDiv = document.createElement('div');
    alertDiv.className = `alert alert-${type} alert-dismissible fade show position-fixed`;
    alertDiv.style.cssText = 'top: 20px; right: 20px; z-index: 9999; min-width: 300px;';
    alertDiv.innerHTML = `
        ${message}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    `;
    
    document.body.appendChild(alertDiv);
    
    // 自动移除警告
    setTimeout(() => {
        if (alertDiv.parentNode) {
            alertDiv.parentNode.removeChild(alertDiv);
        }
    }, 5000);
}
</script>
{% endblock %}